import {
  setSession,
  getSession,
  setLocalSession,
  getLocalSession,
} from "../utils/index.js";
export default {
  namespaced: true,
  state: {
    searchHistory: getLocalSession("searchHistory") || [],
    resultList:getSession("resultList")||[]
  },
  getters: {},
  mutations: {
    setHistory(state, result) {

      if (result) {
        console.log(state.searchHistory.length);
        let index = state.searchHistory.indexOf(result);
        if (state.searchHistory.length < 10) {
          if (index != -1 && state.searchHistory.length > 0) {
            state.searchHistory.splice(index, 1);
            state.searchHistory.unshift(result);
            console.log(state.searchHistory);
          } else {
            state.searchHistory.unshift(result);
            console.log(state.searchHistory);
          }
        } else {
          if (index != -1 ) {
            state.searchHistory.splice(index, 1);
            state.searchHistory.unshift(result);
            console.log(state.searchHistory);
          } else {

            state.searchHistory.splice(9, 1);
            state.searchHistory.unshift(result);
            console.log(state.searchHistory);
          }
       
        }
      }

      // else if(state.searchHistory.length=0) {
      //   state.searchHistory[0]=result
      //   console.log(state.searchHistory);
      // }
      setLocalSession("searchHistory", state.searchHistory);
    },
    clearSearch(state) {
      state.searchHistory = [];
      console.log(state.searchHistory);
      localStorage.removeItem("searchHistory")
    },
    delteSearch(state,index) {
      state.searchHistory.splice(index, 1);
    },
    setResultList(state,resultList) {
      setSession("resultList",resultList)
      state.resultList=resultList
    },
    clearResultList(state,resultList){
      state.resultList=[]
      sessionStorage.removeItem('resultList')
    }
  },
  actions: {},
};
